package com.itaming.lycheeframework.logging.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.itaming.lycheeframework.logging.aspect.RequestLogAspect;
import com.itaming.lycheeframework.logging.properties.LycheeLoggingProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * Logging模块配置类
 *
 * @author A.Ming
 */
@AutoConfiguration
@EnableConfigurationProperties({LycheeLoggingProperties.class})
@RequiredArgsConstructor
public class LycheeLoggingConfiguration {

    /**
     * Logging配置参数
     */
    private final LycheeLoggingProperties loggingProperties;

    /**
     * ObjectMapper
     */
    private final ObjectMapper objectMapper;

    /**
     * 请求日志切面
     */
    @Bean
    @ConditionalOnProperty(prefix = LycheeLoggingProperties.PREFIX, name = "request.enabled", havingValue = "true", matchIfMissing = true)
    public RequestLogAspect requestLoggingAspect() {
        return new RequestLogAspect(objectMapper, loggingProperties.getRequest());
    }

}
